﻿/*
   Copyright 2012 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Flower.Client;
using Flower.Directory.Host.Default;
using Flower.Services.Data;
using Flower.Testing;
using NUnit.Framework;
using Jurassic;

namespace Flower.Directory.Scripting.Tests
{
    [TestFixture]
    public class UtilTests
    {
        private ScriptEngine _engine;

        [SetUp]
        public void Setup()
        {
            _engine = Scripting.Setup.CreateEngine();
        }

        [Test]
        public void AncestorPath()
        {
            _engine.Execute("var path = 'Path1/Path2/Path3';");
            Assert.IsTrue(_engine.Evaluate<string>("path.ancestorPath(1)") == "Path1/Path2");
            Assert.IsTrue(_engine.Evaluate<string>("path.ancestorPath(2)") == "Path1");
            Assert.IsTrue(_engine.Evaluate("path.ancestorPath(3)") is Undefined);
        }

        [Test]
        public void GetResolveLinks()
        {
            var dir = new MemoryDirectory.Directory(new ScriptEnginePool(1));
            
            new DirectoryBuilder(dir)
                .Root("/Assemblies")
                    .Folder("MyAssembly")
                        .Assembly("1.0.0.0", new byte[] { }).End()
                        .Link("Latest", dir.Get("/Assemblies/MyAssembly/1.0.0.0").Id).End()
                    .End()
                .End();

            var engine = Scripting.Setup.CreateEngine();
            engine.SetGlobalValue("$dir", new DirectoryObject(engine, dir));

            var entry = 
                engine.Evaluate("__getResolveLinks('/Assemblies/MyAssembly/1.0.0.0')") as DirectoryEntryObject;
            
            Assert.IsTrue(entry.Entry is Assembly);
        }
    }
}
